/**
 * Custom ArrayAdapter sebagai ListView terkustomisasi
 * Isinya nanti adalah LevelListViewItem
 */

package dp.mobile.game;

import java.text.DateFormat;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import dp.mobile.eden.R;

public class ScoreAdapter extends ArrayAdapter<Score>{
	public ScoreAdapter(Activity context, List<Score> list){
		super(context, R.layout.skirmish_level_item, list);
		
		mContext	= context;
		mList		= list;
	}
	
	static class ViewHolder {
		protected TextView 	playerName;
		protected TextView	dateTaken;
		protected TextView	scoreValue;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder		= null;
		LayoutInflater inflator		= mContext.getLayoutInflater();
		final Score currItem 		= getItem(position);
		
		//Always Override convertView (not efficient but it keeps the ListView item consistent)  
		convertView	= inflator.inflate(R.layout.score_item, null);
	
		//Bind convertView to viewHolder
		viewHolder	= new ViewHolder();
		convertView.setTag(viewHolder);
		
		//Get the View
		viewHolder.playerName   = (TextView)	convertView.findViewById(R.id.player_name);
		viewHolder.dateTaken	= (TextView)	convertView.findViewById(R.id.date_taken);
		viewHolder.scoreValue	= (TextView)	convertView.findViewById(R.id.score);
		
		//Set the View
		viewHolder.playerName.setText(currItem.getPlayerName());
		viewHolder.dateTaken.setText(DateFormat.getDateTimeInstance().format(currItem.getDateTaken()));
		viewHolder.scoreValue.setText(String.valueOf(currItem.getScoreValue()));
		
		return convertView;
	}
	
	//ATTRIBUTES
	private final List<Score> mList;
	private final Activity mContext;
}
